<?php

class PhpFox_Mod_Messenger_ComponentComments extends BaseComponent
{
    function process()
    {
    	$oReq = &App::getRequest();

    	$oSrvMes = &$this->_oModule->getService('Messenger');

    	$aAction = $oReq->get('action');

    	switch($aAction)
    	{
    		case 'view':
    		if ( $iId = $oReq->get('id') )
    		{
    			$aItem = $oSrvMes->getAlertView($iId);
    		}
    		
    		if (!count($aItem))
    		{
    			App::gotoUrl('public.account.messages.alert', array());
    		}
    		
    		break;
    		case 'send':
    		if ( $iId = $oReq->get('id') )
    		{
    			if ( $sUrl = $oSrvMes->getAlert($iId, "", $oReq->get('isgb'), $oReq->get('quote')) )
    			{
    				header("Location: ". $sUrl ."");
    				exit;
    			}
	    	}
    		break;
    		default:

    		if ($iId = $oReq->get('deleteid'))
    		{
    			$oSrvMes->deleteAlert($iId);
    			$bIsDeleted = true;
    		}
    			
    		if ( $iIds = $oReq->getArray('id') )
    		{
    			foreach($iIds as $iId)
    			{
    				$oSrvMes->deleteAlert($iId);
    			}

    			$bIsDeleted = true;
    		}

    		$iPage = $oReq->get('page', 1);
        	$iPageSize = App::getParam('public.pagesize');
        	$sOrder = 'time DESC';

    		list($aAlerts, $iCnt) = $oSrvMes->listAlerts(App::getUser('id'), $iPage, $iPageSize, $sOrder);

    		$oPager = App::newObject('Pager', $iCnt, $iPage, $iPageSize);

    		break;
    	}

        $this->oTpl->assign(array
        (
        	'bIsDeleted' 	=> (isset($bIsDeleted) ? true : false),
        	'aItem' 		=> (isset($aItem) ? $aItem : ''),
        	'aAction' 		=> $aAction,
			'aAlerts'   	=> (isset($aAlerts) ? $aAlerts : ''),
			'iCnt'         	=> (isset($iCnt) ? $iCnt : ''),
        	'aPaging'      	=> (isset($oPager) ? $oPager->getInfo(App::getUrl()) : ''),
        ));

		$sHtml = $this->oTpl->fetch('Comments.html');
		return $sHtml;
	}
}

?>